package com.kitty.game.equip.handler;

import com.kitty.game.FieldValuePosConst;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.role.model.Role;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.*;

/**法宝洗练*/
@Component("EquipSkillArtifactHandler")
public class EquipSkillArtifactHandler extends UpgradeHandler {
    private static final String NEED_STONE_NOT_LEVEL = "你身上#R{0}#n不足#R{1}#n块，不能升级。";
    private final static String ERROR_ITEM = "放入的物品不是法宝，请重新选择。";
    private final static String NOT_MATCH = "请放入同种类法宝。";
    private final static String GOLD_NOT_ENOUGH = "你的元宝不足。";
    private final static String LEVEL_NOT_ENOUGH = "法宝特技等级不能超过自身等级。";
    private final static String SUCCESS = "法宝洗练成功。";
    private final static int NEED_GOLD = 466;

    private final static List<String> SKILLS = Arrays.asList("diandao_qiankun", "jingangquan", "wuji_bifan", "tianyan", "chaofeng", "qinmi_wujian");

    /**
     * 开始打造 返回打造成功或者失败
     *
     * @param role
     * @param params
     */
    @Override
    public String doUpgrade(Role role, Object... params) {
        //主法宝
        RoleEquip mainArtifact = (RoleEquip) params[0];
        RoleEquip firstEquip = (RoleEquip) params[1];
        RoleEquip secondEquip = (RoleEquip) params[2];
        RoleEquip itemName =  (RoleEquip) params[3];


        int specialSkillLevel = mainArtifact.getEquipValue(FieldValuePosConst.ARTIFACT_SKILL_LEVEL);
        //根据等级获取消耗的物品
        Map<String, Integer> needItemByLevel = getNeedItemByLevel(specialSkillLevel);
        /**删除副法宝*/
        equipService.delRoleEquip(firstEquip,role);
        if(specialSkillLevel >= 10){
            /**删除副法宝*/
            equipService.delRoleEquip(secondEquip,role);
        }
        //删除物品
        for (Map.Entry<String, Integer> stringIntegerEntry : needItemByLevel.entrySet()) {
            int stoneNum = equipService.getCount(stringIntegerEntry.getKey(), true, role);
            if (stringIntegerEntry.getValue() > stoneNum) {
                return MessageFormat.format(NEED_STONE_NOT_LEVEL, stringIntegerEntry.getKey(),stringIntegerEntry.getValue());
            }
            int limitNum = equipService.delRoloEquip(stringIntegerEntry.getKey(), stringIntegerEntry.getValue(), true, role);
        }
//        mainArtifact.setEquipValue(FieldValuePosConst.ARTIFACT_SKILL, SKILLS.get(new Random().nextInt(SKILLS.size())));
        //特技等级+1
        mainArtifact.setEquipValue(FieldValuePosConst.ARTIFACT_SKILL_LEVEL, specialSkillLevel+1);
         specialSkillLevel =specialSkillLevel+1;
        if(specialSkillLevel>=1){
            roleService.addAchieveScore(role,"303020");
        }
        if(specialSkillLevel>=2){
            roleService.addAchieveScore(role,"303030");
        }
        if(specialSkillLevel>=3){
            roleService.addAchieveScore(role,"303045");
        }
        if(specialSkillLevel>=4){
            roleService.addAchieveScore(role,"303006");
        }
        if(specialSkillLevel>=5){
            roleService.addAchieveScore(role,"303022");
        }
        if(specialSkillLevel>=6){
            roleService.addAchieveScore(role,"303032");
        }
        if(specialSkillLevel>=7){
            roleService.addAchieveScore(role,"303028");
        }
        if(specialSkillLevel>=8){
            roleService.addAchieveScore(role,"303040");
        }
        if(specialSkillLevel>=9){
            roleService.addAchieveScore(role,"303001");
        }
        if(specialSkillLevel>=10){
            roleService.addAchieveScore(role,"303011");
        }
        if(specialSkillLevel>=11){
            roleService.addAchieveScore(role,"303047");
        }
        if(specialSkillLevel>=12){
            roleService.addAchieveScore(role,"303034");
        }
        if(specialSkillLevel>=13){
            roleService.addAchieveScore(role,"303021");
        }
        if(specialSkillLevel>=14){
            roleService.addAchieveScore(role,"303008");
        }
        if(specialSkillLevel>=15){
            roleService.addAchieveScore(role,"303044");
        }
        if(specialSkillLevel>=16){
            roleService.addAchieveScore(role,"303031");
        }
        if(specialSkillLevel>=17){
            roleService.addAchieveScore(role,"303018");
        }
        if(specialSkillLevel>=18){
            roleService.addAchieveScore(role,"303004");
        }
        if(specialSkillLevel>=19){
            roleService.addAchieveScore(role,"303042");
        }
        if(specialSkillLevel>=20){
            roleService.addAchieveScore(role,"303036");
        }
        if(specialSkillLevel>=21){
            roleService.addAchieveScore(role,"303024");
        }
        if(specialSkillLevel>=22){
            roleService.addAchieveScore(role,"303010");
        }
        if(specialSkillLevel>=23){
            roleService.addAchieveScore(role,"303046");
        }
        if(specialSkillLevel>=24){
            roleService.addAchieveScore(role,"303033");
        }
        refreshRoleEquip(role, mainArtifact);
        pushOperateResult(role,UPGRADE_SUCCESS);
        return SUCCESS;
    }

    /**
     * 入口
     *
     * @param role
     * @param upgradePos
     * @param param
     */
    @Override
    public String acceptUpgrade(Role role, int upgradePos, String param) {
        String[] params = param.split("\\|");
        RoleEquip mainArtifact = equipService.getRoleEquipByPos(role, upgradePos);
        RoleEquip firstEquip = equipService.getRoleEquipByPos(role, Integer.parseInt(params[0]));
        RoleEquip secondEquip = equipService.getRoleEquipByPos(role, Integer.parseInt(params[1]));
        RoleEquip itemEquip = equipService.getRoleEquipByPos(role, Integer.parseInt(params[2]));
        /**判断是不是法宝*/
        if (!isArtifact(mainArtifact, firstEquip)) {
            return ERROR_ITEM;
        }


        /**判断副法宝跟主法宝是不是同种类*/
        if(!mainArtifact.getName().equals(firstEquip.getName())){
            return NOT_MATCH;
        }
        //如果技能大于10级了 要判断两个法宝了。
        int specialSkillLevel = mainArtifact.getEquipValue(FieldValuePosConst.ARTIFACT_SKILL_LEVEL);
        if(specialSkillLevel>=10){
            if (!isArtifact(secondEquip)) {
                return ERROR_ITEM;
            }
            if(!mainArtifact.getName().equals(secondEquip.getName())){
                return NOT_MATCH;
            }
        }

        /**判断主法宝登记是否足够*/
        if(mainArtifact.queryLevel()<= specialSkillLevel){
           return LEVEL_NOT_ENOUGH;
        }
//
//        /**判断元宝是否足够*/
//        if(role.getGold()<NEED_GOLD){
//            return GOLD_NOT_ENOUGH;
//        }


        return doUpgrade(role,mainArtifact,firstEquip,secondEquip,itemEquip);
    }
    /**
     * 根据当前法宝等级 获取需要升级的材料。
     */
    private Map<String,Integer>  getNeedItemByLevel(int level){
        Map<String,Integer> needItem = new HashMap();
        if(level <=4){
            needItem.put("蓝松石",2);

        }
        if(level >4 && level <=9){
            needItem.put("芙蓉石",2);

        }
        if(level >9 && level <=14){
            needItem.put("红宝石",1);

        }
        if(level >14 ){
            needItem.put("蓝宝石",1);

        }
        return needItem;
    }
    /**
     * 判断传入的是不是法宝
     */
    private boolean isArtifact(RoleEquip... roleEquips) {
        for (RoleEquip roleEquip : roleEquips) {
            if (roleEquip == null || !roleEquip.isArtifact()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 检查需要的资源是否足够
     *
     * @param objects
     */
    @Override
    public String checkResource(Object... objects) {
        return null;
    }

    /**
     * 打造需要的金钱
     * pos 只在鉴定的时候用
     *
     * @param level
     * @param pos
     */
    @Override
    public int needCash(short level, int pos) {
        return 0;
    }

    /**
     * 取操作类型的字符串
     */
    @Override
    public String getUpdateTypeStr() {
        return "refine_artifact";
    }
}
